//
//  HWGameCommunityListCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/18.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameCommunityListCell: UICollectionViewCell {
    
    var bgView = c_make_ex(UIView.self)
        .bgColor(.white).end()
    var imageView:UIImageView!
    var playImage:UIImageView!
    var model:HWGameCommunityListModel? {
        didSet {
            if (model?.video.count ?? 0) > 0 {
                playImage.isHidden = false
            } else {
                playImage.isHidden = true
            }
            if (model?.video.count ?? 0) > 0 {
                imageView.snp.updateConstraints { make in
                    make.trailing.equalToSuperview().offset(-18*widthScale)
                    make.leading.equalToSuperview().offset(18*widthScale)
                }
            } else {
                if (model?.image.count ?? 0 ) > 0 {
                    if (model?.image.count ?? 0 ) == 1 {
                        imageView.snp.updateConstraints { make in
                            make.trailing.equalToSuperview().offset(-149*widthScale)
                            make.leading.equalToSuperview().offset(18*widthScale)
                        }
                    } else {
                        imageView.snp.updateConstraints { make in
                            make.trailing.equalToSuperview().offset(-18*widthScale)
                            make.leading.equalToSuperview().offset(18*widthScale)
                        }
                    }
                    
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameCommunityListCell {
    private func initUI() -> Void {
        
        backgroundColor = .white
        contentView.backgroundColor = .white
        
        
        contentView.addSubview(bgView)
        
        imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 4*widthScale
        imageView.layer.masksToBounds = true
        bgView.addSubview(imageView)
        
        playImage = UIImageView()
        playImage.image = UIImage(named: "playVideo")
        imageView.addSubview(playImage)
        playImage.isHidden = true
    }
    
    private func initFrame() -> Void {
        
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        imageView.snp.makeConstraints { make in
            make.leading.trailing.top.bottom.equalToSuperview()
        }
        
        playImage.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
}
